package com.example.babara.memo.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.babara.memo.R;
import com.example.babara.memo.pojo.Memo;
import com.example.babara.memo.utils.MemoAdapter;
import com.example.babara.memo.utils.DatabaseHelper;
import java.util.ArrayList;

public class MainActivity extends Activity implements View.OnClickListener{
    private ImageView addImage;
    private ListView listView;
    private ArrayList<Memo> list;
    private DatabaseHelper memoDbHelper;
    private ImageView mainBackImage;
    private TextView settingsTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        addImage = (ImageView)findViewById(R.id.add_button);
        mainBackImage = (ImageView)findViewById(R.id.main_back_image);
        settingsTextView = (TextView)findViewById(R.id.settings);

        addImage.setOnClickListener(this);
        settingsTextView.setOnClickListener(this);
        mainBackImage.setOnClickListener(this);
        list = new ArrayList<>();
        setMemoList();

    }

    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.add_button:
                Intent intent1 = new Intent(MainActivity.this, AddMemoActivity.class);
                startActivity(intent1);
                this.finish();
                break;
            case R.id.settings:
                Intent intent2 = new Intent(MainActivity.this, SettingsActivity.class);
                startActivity(intent2);
                break;
            case R.id.main_back_image:
                onBackPressed();
                break;
        }
    }


    private void setMemoList(){
        memoDbHelper = new DatabaseHelper(this, "memo.db", null, 1);
        SQLiteDatabase db = memoDbHelper.getWritableDatabase();
        Cursor cursor = db.query("memo", null, null, null, null, null, null);
        list.clear();
        if(cursor.moveToFirst()){
            do{
                //遍历Cursor对象，取出数据并打印
                int memoId = cursor.getInt(cursor.getColumnIndex("id"));
                int startHour = cursor.getInt(cursor.getColumnIndex("start_hour"));
                int startMin = cursor.getInt(cursor.getColumnIndex("start_main"));
                String memoRing = cursor.getString(cursor.getColumnIndex("memo_ring"));
                String memoCard = cursor.getString(cursor.getColumnIndex("memo_card"));
                String memoRepeat = cursor.getString(cursor.getColumnIndex("memo_repeat"));
                String memoScope = cursor.getString(cursor.getColumnIndex("memo_scope"));
                String memoRemark = cursor.getString(cursor.getColumnIndex("memo_remark"));
                Memo tempMemo= new Memo(memoId, startHour, startMin, memoRing, memoCard, memoRepeat, memoScope, memoRemark);
                list.add(tempMemo);
            }while (cursor.moveToNext());
        }

        MemoAdapter memoAdapter = new MemoAdapter(MainActivity.this, R.layout.memo_item, list);
        listView = (ListView) findViewById(R.id.my_memo_list);
        listView.setAdapter(memoAdapter);

        //点击列表中的每一条，会将该条目的信息展示在上部分，以共商家查看详细信息，确定发放或者取消
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final Memo memo1 = (Memo)listView.getItemAtPosition(position);
                clickItemEvent(memo1);
            }
        });

    }

    public void clickItemEvent(final Memo memo){
        final Memo operateMemo = memo;
        AlertDialog.Builder builder=new AlertDialog.Builder(this);  //先得到构造器
        builder.setTitle("提示"); //设置标题
        builder.setMessage("您确对该条备忘进行得操作是：？"); //设置内容
        builder.setPositiveButton("编辑", new DialogInterface.OnClickListener() { //设置确定按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(MainActivity.this, AddMemoActivity.class);
                intent.putExtra("memo", operateMemo);
                startActivity(intent);
                dialog.dismiss(); //关闭dialog
            }
        });
        builder.setNegativeButton("删除", new DialogInterface.OnClickListener() { //设置取消按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                deleteMemo(operateMemo.getId());
                Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                setMemoList();
            }
        });

        //参数都设置完成了，创建并显示出来
        builder.create().show();
    }

    public void deleteMemo(int id){
        SQLiteDatabase db =memoDbHelper.getWritableDatabase();
        db.delete("memo", "id = ?", new String[]{String.valueOf(id)});
    }


    @Override
    public void onBackPressed() {

//      super.onBackPressed();
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("提示").setMessage("您确定要退出客户端吗？")
                    .setIcon(R.drawable.cry);
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    finish();
                    System.exit(0);
                    // android.os.Process.killProcess(android.os.Process.myPid());
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {

                }
            });
            Dialog dialog = builder.create();
            dialog.show();
            dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);
            return;
        }
    }
